<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/17
 * Time: 13:42
 */

namespace app\admin\controller\user;
use app\admin\model\wallet\Record;
use app\common\controller\Backend;

class Flowingwater extends Backend
{
    /**
     * Record模型对象
     * @var \app\admin\model\wallet\Record
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\WaterMultiple;
        //$this->view->assign("payStatusList", $this->model->getPayStatusList());
    }
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $data = request()->get();
            if( !json_decode($data['filter'],true) ){
                $result = array("total" => 0, "rows" => []);
                return json($result);
            }


            $user_id = \app\admin\model\User::where('username',json_decode($data['filter'],true)['username'])->value('id');

            if( !$user_id ){
                $result = array("total" => 0, "rows" => []);
                return json($result);
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $upstores = $this->model
                ->with(['record'])
                ->where('user_id',$user_id)
                ->where('change_money','>',0)
                ->where('type','in',[1,5,7,8])
                ->limit(2)
                ->order('id','desc')
                //->where($where)
                //->where('type','in',[1,3,5,7,8,11])
                ->select();

            $c = [];
            if( count($upstores) == 1 ){
                $list = $this->model
                    ->with(['record'])
                    ->where('user_id',$user_id)
                    ->where('id','>=',$upstores[0]['id'])
                    ->limit($offset, $limit)
                    ->order('id','asc')
                    ->select();
                $total = $this->model
                    ->with(['record'])
                    ->where('user_id',$user_id)
                    ->where('id','>',$upstores[0]['id'])
                    ->count();
            }elseif(count($upstores) == 2  ){
                if( ($upstores[0]['record']['createtime'] - $upstores[1]['record']['createtime']) <= 900 ){

                    $list = $this->model
                        ->with(['record'])
                        ->where('user_id',$user_id)
                        ->where('id','>=',$upstores[1]['id'])
                        ->limit($offset, $limit)
                        ->order('id','asc')
                        ->select();
                    $total = $this->model
                        //->with(['user','record'])
                        ->where('user_id',$user_id)
                        ->where('id','>=',$upstores[1]['id'])
                        ->count();
                }else{
                    $list = $this->model
                        ->with(['record'])
                        ->where('user_id',$user_id)
                        ->where('id','>=',$upstores[0]['id'])
                        ->limit($offset, $limit)
                        ->order('id','asc')
                        ->select();
                    $total = $this->model
                        ->with(['record'])
                        ->where('user_id',$user_id)
                        ->where('id','>=',$upstores[0]['id'])
                        ->count();
                }
            }else{
                $result = array("total" => 0, "rows" => []);
                return json($result);
            }


            $arr = collection($list)->toArray();
            foreach ( $arr as $k2=>$v2 ){
                if( $v2['change_money'] > 0 ){
                    $arr[$k2]['bet'] = 0;
                    $arr[$k2]['recharge'] = $v2['change_money'];
                }else{
                    $arr[$k2]['bet'] = abs($v2['change_money']);
                    $arr[$k2]['recharge'] = 0;
                }
                if( $v2['type'] == 1 ){
                    $arr[$k2]['type_name'] = '微信充值';
                }elseif( $v2['type'] == 3 ){
                    $arr[$k2]['type_name'] = '下注';
                }elseif( $v2['type'] == 5 ){
                    $arr[$k2]['type_name'] = '赠送彩金';
                }elseif( $v2['type'] == 8 ){
                    $arr[$k2]['type_name'] = '支付宝充值';
                }elseif( $v2['type'] == 7 ){
                    $arr[$k2]['type_name'] = '银行转账';
                }elseif( $v2['type'] == 12){
                    $arr[$k2]['type_name'] = '撤单';
                }
                $arr[$k2]['username'] = json_decode($data['filter'],true)['username'];
            }
            $result = array("total" => $total, "rows" => $arr);
            return json($result);

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['user','record'])
                ->where($where)
                ->where('type','in',[1,3,5,7,8,11])
                ->select();

            $a = [];
            $liushui = 0;
            $num = 0;
            $b = [];
            foreach ($list as $k=>$v){
                if( in_array($v['type'],[1,5,7,8,11]) ){
                    $a[$num]['user_id'] = $v['user_id'];
                    if( $v['type'] == 5 ){
                        $a[$num]['type_name'] = '后台赠送';
                        $a[$num]['liushui'] = 0;//有效投注
                    }elseif($v['type'] == 11){
                        $a[$num]['type_name'] = '余额宝转入钱包';
                        $a[$num]['liushui'] = -$liushui;//有效投注
                    }else{
                        $a[$num]['type_name'] = '充值';
                        $a[$num]['liushui'] = -$liushui;//有效投注
                    }

                    $a[$num]['username'] = $v['user']['username'];
                    $a[$num]['recharge'] = $v['change_money'];

                    $a[$num]['mustliushui'] = $v['change_money'] * $v['multiples'];//一倍流水
                    $a[$num]['createtime'] =  $v['record']['createtime'];
                    $num++;
                }
                if( $v['type'] == 3 ){
                    if( isset($a[$num-1]) ){
                        $liushui = -$v['change_money'];
                        array_push($b,$v['user_id']);
                        $a[$num-1]['liushui'] += $liushui;
                    }
                }
            }
            foreach ( $a as $k1 => $v1 ){
                //同一笔存款既有充值也有赠送的情况下，将赠送的流水重置
                if( $v1['type_name'] == '后台赠送' && ($k1-1) >= 0 && $a[$k1-1]['type_name'] == '充值' ){
                    if( $v1['liushui'] > $v1['mustliushui'] ){
                        $a[$k1]['liushui']   = $v1['mustliushui'];
                        $a[$k1-1]['liushui'] = $v1['liushui'] - $v1['mustliushui'];
                    }
                }
            }
            foreach ( $a as $key=>$value ){
                if( $value['liushui'] >= $value['mustliushui'] ){
                    $m = number_format($value['liushui']-$value['mustliushui'],2);
                    $a[$key]['valid'] = '是('.$m.')';
                }else{
                    $m = $value['liushui']-$value['mustliushui'];
                    $a[$key]['valid'] = $m;
                }
                $a[$key]['liushui'] = number_format($value['liushui'],2);
                $a[$key]['mustliushui'] = number_format($value['mustliushui'],2);
            }
            $result = array("total" => count($a), "rows" => $a);
            return json($result);

        }
        return $this->view->fetch();
    }

}